define(function (require, exports, module) {

    // 通过 require 引入依赖
    // var $ = require('jquery');
    require('common');
    require('sui');
    var template = require('template');
    dateFormat(template);
    var rows = 20;

    function initData() {
        getChannelData(1, rows);
        getUserData(1, rows);
        getGroupData(1, rows);
    }

    $(document).on('click', '.del-msg', function () {
        var msgId = $(this).attr('data-msgid');
        var ids = new Array();
        ids[0] = parseInt(msgId);
        $.post('/userMessages/delMsgAll', {ids: JSON.stringify(ids)}, function (data) {
            switch (data.code) {
                case 0:
                    toastSuccess('删除成功');
                    setTimeout(function () {
                        window.location.href = '/userMessages';
                    }, 1000);
                    break;
                default :
                    toastError('删除失败');
            }
        });
    });

    $(document).on('click', '.query-channel', function () {
        getChannelData(1, rows);
    });

    $(document).on('click', '.query-user', function () {
        getUserData(1, rows);
    });

    $(document).on('click', '.query-group', function () {
        getGroupData(1, rows);
    });
    $(document).on('click', '.forward-channel,.forward-group,.forward-user', function () {
        var inpusts;
        var forwardType;
        if ($(this).hasClass('forward-user')) {
            inpusts = $('input[name="user-id"]');
            forwardType = '1';
        } else if ($(this).hasClass('forward-channel')) {
            inpusts = $('input[name="channel-id"]');
            forwardType = '2';
        } else if ($(this).hasClass('forward-group')) {
            inpusts = $('input[name="group-id"]');
            forwardType = '3';
        }
        var ids = new Array();
        $.each(inpusts, function (i, item) {
            if ($(item).is(':checked')) {
                ids.push(parseInt($(item).val()));
            }
        });
        var msgIds = new Array();
        msgIds[0] = $('.del-msg').attr('data-id');
        var data = {
            msgIds: JSON.stringify(msgIds),
            ids: JSON.stringify(ids),
            forwardType: forwardType
        };
        $.post('/userMessages/forwardMsg', data, function (o) {
            switch (o.code) {
                case 0:
                    toastSuccess('转发成功');
                    break;
                default:
                    toastError('转发失败');
            }
        });

    });


    $(document).on('click', '.check-all-channel', function () {
        if ($(this).is(":checked")) {
            $('input[name="channel-id"]').prop("checked", true);//全选
            $('#tab-channel tbody tr').addClass('tr-click');
        } else {
            $('input[name="channel-id"]').prop("checked", false);//取消全选
            $('#tab-channel tbody tr').removeClass('tr-click')
        }
    }).on('click', '.check-all-user', function () {
        if ($(this).is(":checked")) {
            $('input[name="user-id"]').prop("checked", true);//全选
            $('#tab-user tbody tr').addClass('tr-click');
        } else {
            $('input[name="user-id"]').prop("checked", false);//取消全选
            $('#tab-user tbody tr').removeClass('tr-click')
        }
    }).on('click', '.check-all-group', function () {
        if ($(this).is(":checked")) {
            $('input[name="group-id"]').prop("checked", true);//全选
            $('#tab-group tbody tr').addClass('tr-click');
        } else {
            $('input[name="group-id"]').prop("checked", false);//取消全选
            $('#tab-group tbody tr').removeClass('tr-click')
        }
    });

    function getChannelData(page, rows) {
        var channelName = $.trim($('#channelName').val());
        var data = {
            channelName: channelName,
            page: page,
            rows: rows,
            channelStatus: '0'
        };
        $.post('/channel/list', data, function (data) {
            switch (data.code) {
                case 0:
                    var list = data.data.list;
                    var count = data.data.count;
                    var html = template('tp-channel', {list: list});
                    $('#tab-channel tbody').empty();
                    $('#tab-channel tbody').html(html);
                    $('.channel-pagination').pagination('updateItemsCount', count);
                    break;
                default:
                    toastError('查询数据失败');
            }
        });
    }

    function getUserData(page, rows) {
        var accountName = $.trim($('#accountName').val());
        var data = {
            accountName: accountName,
            page: page,
            rows: rows,
            statusCode: '0'
        };
        $.post('/user/list', data, function (data) {
            switch (data.code) {
                case 0:
                    var list = data.data.list;
                    var count = data.data.count;
                    var html = template('tp-user', {list: list});
                    $('#tab-user tbody').empty();
                    $('#tab-user tbody').html(html);
                    $('.user-pagination').pagination('updateItemsCount', count);
                    break;
                default:
                    toastError('查询数据失败');
            }
        });
    }

    function getGroupData(page, rows) {
        var groupName = $.trim($('#groupName').val());
        var data = {
            groupName: groupName,
            page: page,
            rows: rows,
            groupStatus: '0'
        };
        $.post('/group/list', data, function (data) {
            switch (data.code) {
                case 0:
                    var list = data.data.list;
                    var count = data.data.count;
                    var html = template('tp-group', {list: list});
                    $('#tab-group tbody').empty();
                    $('#tab-group tbody').html(html);
                    $('.group-pagination').pagination('updateItemsCount', count);
                    break;
                default:
                    toastError('查询数据失败');
            }
        });
    }


    function dateFormat(template) {
        template.helper('dateFormat', function (date, format) {
            date = new Date(date);
            var map = {
                "M": date.getMonth() + 1, //月份
                "d": date.getDate(), //日
                "h": date.getHours(), //小时
                "m": date.getMinutes(), //分
                "s": date.getSeconds(), //秒
                "q": Math.floor((date.getMonth() + 3) / 3), //季度
                "S": date.getMilliseconds() //毫秒
            };
            format = format.replace(/([yMdhmsqS])+/g, function (all, t) {
                var v = map[t];
                if (v !== undefined) {
                    if (all.length > 1) {
                        v = '0' + v;
                        v = v.substr(v.length - 2);
                    }
                    return v;
                }
                else if (t === 'y') {
                    return (date.getFullYear() + '').substr(4 - all.length);
                }
                return all;
            });
            return format;
        });
    }

    initData();

});